load Factors;

for cntry = [55 50 51]
    ToDisp = cell(3,1);

    %------------------------------------------------------------------
    %Run regression with only R11 on the RHS
    FMcoef = NaN(T,1); Nstocks = NaN(T,1); adjR2 = NaN(T,1);
    for t = TFirst:T
        Y1 = Return(:,t) - BetasFull(:,2:6)*FacDev(t,1:5)';
        Y2 = Return(:,t) - BetasFull(:,2:6)*FacEmg(t,1:5)';
        Y = Y1;
        Y(CountryRegionDummy(:,51)) = Y2(CountryRegionDummy(:,51));

        X = [Return11(:,t-2) MV(:,t-1)];
        idx = isfinite(Y) & all(isfinite(X),2) & CountryRegionDummy(:,cntry) & ~IsREIT & ~IsMicro(:,t-2,cntry);
        if sum(idx)>MinStocks
            % winsorize across all stocks
            if WinsorizeYRet
                y = winsorize(Y(idx),WinsorLevelY);
            else
                y = Y(idx);
            end
            x = winsorize(X(idx,:),WinsorLevelX);

            % neutralize country effects
            K = size(X,2);
            for cc = 1:49
                idx_cc = CountryRegionDummy(idx,cc);
                if sum(idx_cc) >= 1
                    for k = 1:K
                        x(idx_cc,k) = x(idx_cc,k) - mean(x(idx_cc,k),'omitnan');
                    end
                end
            end

            % standardize across all stocks
            xs = standardize(x);
            xx = [xs(:,1) ones(length(y),1)];

            if any(sum(isfinite(xx))==0); continue; end
            if rank(xx) == size(xx,2)
                [bb,adjR2(t)] = myols_simple(xx,y); Nstocks(t) = length(y);
                FMcoef(t,:) = bb(1);
            end
        end
    end
    for sample = 1:3
        switch sample
            case 1, T1 = TFirst; T2 = T;
            case 2, T1 = TFirst; T2 = THalf;
            case 3, T1 = THalf+1; T2 = T;
        end
        m = squeeze(mean(FMcoef(T1:T2,:),'omitnan')); s = squeeze(std(FMcoef(T1:T2,:),'omitnan')); n = squeeze(sum(isfinite(FMcoef(T1:T2,:)))); tstat = sqrt(n).*m./s;
        ToDisp{sample} = [100*m(1);tstat(1);NaN(4,1); mean(Nstocks(T1:T2,:),'omitnan'); 100*mean(adjR2(T1:T2,:),'omitnan')];
    end

    %------------------------------------------------------------------
    %Run regression with R11, X, and R11*X on the RHS
    for xvar = 1:7
        FMcoef = NaN(T,3); Nstocks = NaN(T,1); adjR2 = NaN(T,1);
        for t = TFirst:T
            Y1 = Return(:,t) - BetasFull(:,2:6)*FacDev(t,1:5)';
            Y2 = Return(:,t) - BetasFull(:,2:6)*FacEmg(t,1:5)';
            Y = Y1;
            Y(CountryRegionDummy(:,51)) = Y2(CountryRegionDummy(:,51));

            X = [Return11(:,t-2) XVARS(:,t-1,xvar) MV(:,t-1)];
            idx = isfinite(Y) & all(isfinite(X),2) & CountryRegionDummy(:,cntry) & ~IsREIT & ~IsMicro(:,t-2,cntry);
            if sum(idx)>MinStocks

                % winsorize across all stocks
                if WinsorizeYRet
                    y = winsorize(Y(idx),WinsorLevelY);
                else
                    y = Y(idx);
                end
                x = winsorize(X(idx,:),WinsorLevelX);

                % neutralize country effects
                K = size(X,2);
                for cc = 1:49
                    idx_cc = CountryRegionDummy(idx,cc);
                    if sum(idx_cc) >= 1
                        for k = 1:K
                            x(idx_cc,k) = x(idx_cc,k) - mean(x(idx_cc,k),'omitnan');
                        end
                    end
                end

                % standardize across all stocks
                xs = standardize(x);
                if xvar==3 % convert to residual analyst
                    ytemp = xs(:,2); xtemp = [ones(sum(idx),1) xs(:,3)];
                    xs(:,2) = ytemp - xtemp*(xtemp\ytemp);
                end
                xs2 = [xs(:,1:2) xs(:,1).*xs(:,2)]; % no constant yet
                xx = [xs2 ones(length(y),1)];

                if any(sum(isfinite(xx))==0); continue; end
                if rank(xx) == size(xx,2)
                    [bb,adjR2(t)] = myols_simple(xx,y);
                    FMcoef(t,1:3) = bb(1:3); Nstocks(t) = length(y);
                end
            end
        end
        for sample = 1:3
            switch sample
                case 1, T1 = TFirst; T2 = T;
                case 2, T1 = TFirst; T2 = THalf;
                case 3, T1 = THalf+1; T2 = T;
            end
            m = squeeze(mean(FMcoef(T1:T2,:),'omitnan')); s = squeeze(std(FMcoef(T1:T2,:),'omitnan')); n = squeeze(sum(isfinite(FMcoef(T1:T2,:)))); tstat = sqrt(n).*m./s;
            D1 = []; for i = 1:3; D1 = [D1;100*m(i);tstat(i)]; end; D1 = [D1; mean(Nstocks(T1:T2,:),'omitnan'); 100*mean(adjR2(T1:T2,:),'omitnan')];
            ToDisp{sample} = [ToDisp{sample} D1];
        end
    end

    D = [ToDisp{1} NaN(8,1) ToDisp{2} NaN(8,1) ToDisp{3}];
    if cntry == 55
        writematrix(D,FileName, 'Sheet', SheetName, 'Range',Range1, 'AutoFitWidth',false);
    elseif cntry == 50
        writematrix(D,FileName, 'Sheet', SheetName, 'Range',Range2, 'AutoFitWidth',false);
    elseif cntry == 51
        writematrix(D,FileName, 'Sheet', SheetName, 'Range',Range3, 'AutoFitWidth',false);
    end
end
